package com.new1cloud.file.domain;

import com.new1cloud.file.upload.client.entry.ChunkFile;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 分片文件上传是否成功，来自于Server端的消息
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ChunkFileUploadStatus implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 文件名
     */
    private String fileName;
    /**
     * 文件唯一标识码
     */
    private String fileHash;
    /**
     * 分片文件的偏移量
     */
    private Long offset;

    /**
     * 分片文件名，从'1'开始依次往后
     */
    private String chunkName;

    /**
     * 是否已上传到服务端，0表示上传失败，1表示上传成功
     */
    private Integer uploadStatus;

    /**
     * 上传完成的百分比值
     */
    private Integer uploadPercentage;

    /**
     * 当前文件的上传进度详情
     */
    private FileUploadProcess fileUploadProcess;

    /**
     * ChunkFile -> ChunkFileUploadStatus
     */
    public static ChunkFileUploadStatus convertFromChunkFile(ChunkFile chunkFile, FileUploadProcess fileUploadProcess, Integer uploadStatus){
        return ChunkFileUploadStatus.builder().fileName(chunkFile.getFileName())
                .fileHash(chunkFile.getFileHash())
                .offset(chunkFile.getOffset())
                .chunkName(chunkFile.getChunkName())
                .uploadStatus(uploadStatus)
                .uploadPercentage(FileUploadProcess.gotTransferPercentage(fileUploadProcess))
                .fileUploadProcess(fileUploadProcess)
                .build();
    }

    @Override
    public String toString() {
        return "ChunkFileUploadStatus{" +
                "fileName='" + fileName + '\'' +
                ", uploadPercentage=" + uploadPercentage +
                '}';
    }
}